package com.simplecc.hellojetpack.google;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.simplecc.hellojetpack.MainFragment;
import com.simplecc.hellojetpack.R;
import com.simplecc.hellojetpack.databinding.FragmentAppBarBinding;
import com.simplecc.hellojetpack.databinding.LayoutSimpleTextBinding;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AppBarFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AppBarFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    private FragmentAppBarBinding mBinding;

    public AppBarFragment() {

    }

    public static AppBarFragment newInstance(String param1, String param2) {
        AppBarFragment fragment = new AppBarFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mBinding = FragmentAppBarBinding.inflate(LayoutInflater.from(getContext()), container, false);
        initView();
        return mBinding.getRoot();
    }

    private void initView() {
        mBinding.rvContent.setLayoutManager(new LinearLayoutManager(this.getContext()));
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            data.add(String.valueOf(i));
        }
        SimpleAdapter adapter = new SimpleAdapter(getContext(), data);
        mBinding.rvContent.setAdapter(adapter);

    }

    private static class SimpleAdapter extends RecyclerView.Adapter<SimpleViewHolder> {

        private LayoutInflater layoutInflater;
        private List<String> data;
        private SimpleAdapter.OnSimpleItemClickListener mOnSimpleItemClickListener;

        public SimpleAdapter(Context context, List<String> data) {
            layoutInflater = LayoutInflater.from(context);
            this.data = data;
        }

        @NonNull
        @NotNull
        @Override
        public SimpleViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
            LayoutSimpleTextBinding binding = LayoutSimpleTextBinding.inflate(layoutInflater, parent, false);
            return new SimpleViewHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull @NotNull SimpleViewHolder holder, int position) {
            holder.getBinding().tvContent.setText(data.get(position));
            holder.getBinding().getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnSimpleItemClickListener != null)
                        mOnSimpleItemClickListener.onItemClick(v, position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return data.size();
        }


        public void setOnSimpleItemClickListener(SimpleAdapter.OnSimpleItemClickListener listener) {
            this.mOnSimpleItemClickListener = listener;
        }

        public static interface OnSimpleItemClickListener {

            public void onItemClick(View view, int position);
        }
    }

    private static class SimpleViewHolder extends RecyclerView.ViewHolder {

        private LayoutSimpleTextBinding binding;

        public SimpleViewHolder(LayoutSimpleTextBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public LayoutSimpleTextBinding getBinding() {
            return binding;
        }
    }
}